package com.qch.rabbitmq.demo;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * @Author qch
 * @Date 2021/5/13
 */
public class HelloGetConsumer {
    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        //获取连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //
        /**
         * 直接通过AMQP协议连接
         * 指定协议 amqp://
         * 指定密码 123456
         * 指定host 192.168.142.135
         * 指定端口 5672
         * 指定虚拟主机 %2f
         */
        factory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        /**
         * 指定从哪个消费者消费消息
         * 指定是否自动确认消息 true表示自动确认
         *
         */
        GetResponse getResponse = channel.basicGet("queue.biz", true);
        //获取消息体
        byte[] body = getResponse.getBody();
        System.out.println(new String(body));
        //AMQP.BasicProperties props = getResponse.getProps();
       channel.close();
       connection.close();
    }
}
